package com.niit.musicserve.controller;

import com.niit.musicserve.annotation.RequireRole;
import com.niit.musicserve.entity.Album;
import com.niit.musicserve.service.AlbumService;
import com.niit.musicserve.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/album")
public class AlbumController {
	
	@Autowired
	private AlbumService albumService;
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@RequireRole({"admin"})
	public Result<String> addAlbum(@RequestBody Album  album){
		
		try{
			if(album.getAlbumName() == null || album.getAlbumName().trim().isEmpty()){
				return Result.error("请输入专辑名称");
			}
			
			album.setAlbumName(album.getAlbumName().trim());
			album.setSingerId(album.getSingerId());
			album.setSongId(album.getSongId());
			album.setAlbumSignature(album.getAlbumSignature().trim());
			
			int result = albumService.addAlbum(album);
			if(result > 0){
				return Result.success("添加成功");
			}else{
				return Result.error("添加失败");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return Result.error("添加失败" + e.getMessage());
		}
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@RequireRole({"admin"})
	public Result<String> deleteAlbum(HttpServletRequest  request){
		
		try{
			String albumId = request.getParameter("albumId");
			
			if(albumId == null || albumId.trim().isEmpty()){
				return Result.error("请输入专辑编号");
			}
			
			albumId = albumId.trim();
			
			int result = albumService.deleteAlbum(Integer.parseInt(albumId));
			
			if(result > 0){
				return Result.success("删除成功");
			}else{
				return Result.error("删除失败");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return Result.error("删除失败" + e.getMessage());
		}
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@RequireRole({"admin"})
	public Result<String> updateAlbum(@RequestBody Album  album){
		
		try{
			
			if(album.getAlbumId() == null){
				return Result.error("请输入专辑编号");
			}
			
			if(album.getAlbumName() == null || album.getAlbumName().trim().isEmpty()){
				return Result.error("请输入专辑名称");
			}
			
			album.setAlbumId(album.getAlbumId());
			album.setAlbumName(album.getAlbumName().trim());
			album.setSingerId(album.getSingerId());
			album.setSongId(album.getSongId());
			album.setAlbumSignature(album.getAlbumSignature().trim());
			
			int result = albumService.updateAlbum(album);
			if(result > 0){
				return Result.success("更新成功");
			}else{
				return Result.error("更新失败");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return Result.error("系统错误：" + e.getMessage());
		}
	}
	
	@RequestMapping(value = "/allAlbum", method = RequestMethod.GET)
	public Result<List<Album>> allAlbum(HttpServletRequest  request){
		
		return Result.success(albumService.allAlbum());
	}
	
	@RequestMapping(value = "/albumOfId", method = RequestMethod.GET)
	public Result<Album> albumOfId(HttpServletRequest  request){
		
		String albumId = request.getParameter("albumId");
		
		if(albumId == null || albumId.trim().isEmpty()){
			return Result.error("请输入专辑编号");
		}
		
		albumId = albumId.trim();
		
		return Result.success(albumService.albumOfId(Integer.parseInt(albumId)));
	}
	
	@RequestMapping(value = "/albumOfPic", method = RequestMethod.POST)
	@RequireRole({"admin"})
	public Result<String> albumOfPic(
		@RequestParam("file") MultipartFile file,
		@RequestParam("id") int id
	){
			
			if(file.isEmpty()) {
				return Result.error("请选择文件");
			}
			
			String fileName = System.currentTimeMillis() + file.getOriginalFilename();
			
			String filePath = System.getProperty("user.dir") + "/res/img/albumPic";
			
			File file1 = new File(filePath);
			if (!file1.exists()) {
				file1.mkdirs();
			}
			
			File dest = new File(filePath + System.getProperty("file.separator") + fileName);
			
			String storePicParh = "res/img/albumPic/" + fileName;
			
			try {
				file.transferTo(dest);
				
				Album album = new Album();
				album.setAlbumId(id);
				album.setAlbumPic(storePicParh);
				
				int result = albumService.updateAlbum(album);
				
				if(result > 0){
					return Result.success("上传成功");
				}
				
				return Result.error("上传失败");
				
			} catch (IOException e) {
				return Result.error("上传失败" + e.getMessage());
			}
	}
}
